﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PluginInterface;
using Buccontrols;
using TOP;

namespace Buccaneer
{
    public class ShowEnvironmentVariabeleAction : PluginAction
    {
        public ShowEnvironmentVariabeleAction()
        {
            base.name = "showenvar";
            base.func = new ActionFunction(this.ShowEnvironmentVariable);
            base.pluginDaddy = MainProgram.MainForm;
            base.pars.Add("varname", "The name of the variable");
            base.pars.Add("showSubtree", "optional: parse children for child nodes, and all for all subtreenodes");
        }

        public void ShowEnvironmentVariable(string[] args)
        {
            OptionsCollection oc = Options.Settings.Get(args[0]);
            Options.WriteComment("(i) Variable " + args[0] + " is " + oc.Value);
            if (args.Length <= 1) return;
            if (args[1] != "children" && args[1] != "all")
            {
                return;
            }
            else
            {
                foreach (string s in oc.SubTree)
                {
                    OptionsCollection occ = Options.Settings.Get(args[0] + "." + s);
                    Options.WriteComment(" +  Variable " + args[0] + "." + s + " is " + occ.Value);
                    if (args[1] == "all")
                        ShowSubtree(args[0] + "." + s);
                }
            }
        }

        public void ShowSubtree(string subtree)
        {
            OptionsCollection oc = Options.Settings.Get(subtree);
            foreach (string s in oc.SubTree)
            {
                OptionsCollection occ = Options.Settings.Get(subtree + "." + s);
                Options.WriteComment(" +  Variable " + subtree + "." + s + " is " + occ.Value);
                ShowSubtree(subtree + "." + s);
            }
        }
    }

    public class SaveOptionsAction : PluginAction
    {
        public SaveOptionsAction()
        {
            base.name = "saveopt";
            base.func = new ActionFunction(this.SaveOptions);
        }

        public void SaveOptions(string[] args)
        {
            OptionsCollection.SaveOptions(Options.Settings, Environment.CurrentDirectory + @"\toptest\test.top");
        }
    }
}
